<!--
 * @FileDescription: 窗口
 * @Author: Nevertheless
 * @Date: 2025.4.26
 * @LastEditors: Nevertheless
 * @LastEditTime: 2025.4.26
 -->
<script setup>
/**导入组件**/
import {computed, ref} from 'vue'
import {useRouter} from 'vue-router'


/**导入函数**/
import {useSmallWindowStore} from '@/stores/smallWindow'


/**初始化**/
const router = useRouter()
const smallWindowStore = useSmallWindowStore()
const windowRef = ref(null)

let dragging = false
let offsetY = 0


/**定义函数**/
// 显示时间
const formattedTime = computed(() => {
  const minutes = Math.floor(smallWindowStore.clockTime / 60)
  const seconds = smallWindowStore.clockTime % 60
  return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`
})

// 开始拖动（上下）
const startDrag = (e) => {
  dragging = true
  offsetY = e.clientY - windowRef.value.getBoundingClientRect().top

  const onMouseMove = (e) => {
    if (!dragging) return
    const newTop = e.clientY - offsetY
    windowRef.value.style.top = `${newTop}px`
  }

  const onMouseUp = () => {
    dragging = false
    window.removeEventListener('mousemove', onMouseMove)
    window.removeEventListener('mouseup', onMouseUp)
  }

  window.addEventListener('mousemove', onMouseMove)
  window.addEventListener('mouseup', onMouseUp)
}

// 双击逻辑
const handleDoubleClick = () => {
  smallWindowStore.handleDoubleClick()
}
</script>


<template>
  <div
      v-if="smallWindowStore.showSmallWindow"
      class="small-window"
      @mousedown="startDrag"
      @dblclick="handleDoubleClick"
      ref="windowRef"
  >
    ⏱️ {{ formattedTime }}
  </div>
</template>


<style scoped lang="scss">
.small-window {
  position: fixed;
  top: 100px;
  right: 20px;
  width: 100px;
  height: 50px;
  background-color: #2c3e50;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 18px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  user-select: none;
  cursor: grab;
  z-index: 1000;
  transition: all 0.3s ease;

  &:hover {
    background-color: #34495e;
  }
}

</style>
